package com.springboot.study.tests.designpattern.adapter1;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: guodong
 * @Date: 2021/4/25 14:56
 * @Version: 1.0
 * @Description: 该类对接所有的上游各个入口  这个对接下游全部的实现
 * 因为实现了各个
 *
 *
 */
@Component
public class AnimalHandler<R> {

    @Autowired
    private List<AnimalAdapter<R, String>> animalAdapterList;

    private Map<String, AnimalAdapter<R, String>> adapterMap = new HashMap(16);

    @PostConstruct
    public void init() {
        adapterMap = animalAdapterList.stream().collect(Collectors.toMap(AnimalAdapter::getAnimalName, Function.identity(), (k1, k2) -> k2));
        adapterMap = Collections.unmodifiableMap(adapterMap);
    }


    // 调用下游各个实现接口
    public String handle(String type, R r) {
        String name = adapterMap.get(type).convert(r);
        return name;
    }

}
